﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Data;
using VNet.VisiSuite.VisiAdmin.Models;

namespace VNet.VisiSuite.VisiAdmin.Common.Converters
{
    public class SelectedValuesConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values != null && values[1] is ObservableCollection<RollupDefinitionModel> && values[0] is int)
            {
                var children = (ObservableCollection<RollupDefinitionModel>)values[1];
                var child = (from c in children
                               where c.RollupDefinitionId == (int)values[0] 
                               select c).FirstOrDefault();

                return child;
            }

            if (values != null && values[1] is List<CellModel> && values[0] is int)
            {
                var children = (List<CellModel>)values[1];
                var child = (from c in children
                             where c.CellId == (int)values[0]
                             select c).FirstOrDefault();

                return child;
            }

            if (values != null && values[1] is List<ProductModel> && values[0] is int)
            {
                var children = (List<ProductModel>)values[1];
                var child = (from c in children
                             where c.ProductId == (int)values[0]
                             select c).FirstOrDefault();

                return child;
            }

            if (values != null && values[1] is List<BraiderProductModel> && values[0] is int)
            {
                var children = (List<BraiderProductModel>)values[1];
                var child = (from c in children
                             where c.BraiderProductId == (int)values[0]
                             select c).FirstOrDefault();

                return child;
            }

            if (values != null && values[1] is ObservableCollection<ReasonCodeModel> && values[0] is int)
            {
                var children = (ObservableCollection<ReasonCodeModel>)values[1];
                var child = (from c in children
                             where c.ReasonCodeId == (int)values[0]
                             select c).FirstOrDefault();

                return child;
            }

            return Binding.DoNothing;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            object[] objs = new object[2];

            if (value is RollupDefinitionModel)
            {
                objs[0] = ((RollupDefinitionModel)value).RollupDefinitionId;
                return objs;
            }

            if (value is CellModel)
            {
                objs[0] = ((CellModel)value).CellId;
                return objs;
            }

            if (value is ProductModel)
            {
                objs[0] = ((ProductModel)value).ProductId;
                return objs;
            }

            if (value is BraiderProductModel)
            {
                objs[0] = ((BraiderProductModel)value).BraiderProductId;
                return objs;
            }

            if (value is ReasonCodeModel)
            {
                objs[0] = ((ReasonCodeModel)value).ReasonCodeId;
                return objs;
            }
             return null;
        }
    }
}
